﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.FrameXml.Components.Designers;
using Microsoft.WowAddonStudio.FrameXml.Serialization;
using Microsoft.WowAddonStudio.Services;

namespace Microsoft.WowAddonStudio.FrameXml.Components
{
	/// <summary>
	/// Represents the <see cref="Ui"/> FrameXML element that acts as the root container of a FrameXML hierarchy.
	/// </summary>
	[ToolboxItem(false)]
    [Designer(typeof(UiControlDesigner), typeof(IRootDesigner))]
	public partial class UiControl : FrameXmlControl<Ui>
	{
        private IAddonStudioSettings addonStudioSettings;

        /// <summary>
        /// Initializes a new instance of the <see cref="UiControl"/> class.
        /// </summary>
		public UiControl()
		{
			Name = "Ui";
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UiControl"/> class.
		/// </summary>
		/// <param name="ui">The Ui element.</param>
		public UiControl(Ui ui)
			: base(ui)
		{
			Name = "Ui";
		}

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // Set background based on setting
            addonStudioSettings = GetService(typeof(IAddonStudioSettings)) as IAddonStudioSettings;
            if (addonStudioSettings != null)
            {
                // Initialize Background image file changed event
                addonStudioSettings.BackgroundImageFileChanged += OnBackgroundImageFileChanged;
                this.InitializeBackground(addonStudioSettings.BackgroundImageFile);
            }
        }

		/// <summary>
		/// Initializes the styles.
		/// </summary>
		protected override void InitializeStyles()
		{
			base.InitializeStyles();

			AutoScroll = true;
		}

		/// <summary>
		/// Gets the <see cref="Ui"/>.
		/// </summary>
		public Ui Ui
		{
			get { return SerializationObject as Ui; }
		}

        /// <summary>
        /// Gets or sets a value indicating whether this instance is in virtual state.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is in virtual state; otherwise, <c>false</c>.
        /// </value>
        public bool IsInVirtualState { get; set; }

    	/// <summary>
		/// Called after the control has been added to another container.
		/// </summary>
		protected override void InitLayout()
		{
			base.InitLayout();
			Dock = DockStyle.Fill;
		}

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.SizeChanged"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
		protected override void OnSizeChanged(EventArgs e)
		{
			base.OnSizeChanged(e);

			// Perform layout on Ui and all dependent controls
            PerformLayout("SizeDimension");

			//Update selection frames
			UpdateDesignerSelection();
		}

        /// <summary>
        /// Backgrounds the image file changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public void OnBackgroundImageFileChanged(object sender, EventArgs e)
        {
            this.InitializeBackground(addonStudioSettings.BackgroundImageFile);
        }
	}
}